//
//  main.m
//  07-homework
//
//  Created by dllo on 16/7/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "City.h"
#import "District.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //area文件处理
       NSString *pathOfArea = @"/Users/dllo/DLS160710-09/Objective-C/HomeWork/07-homework/area.txt";
        NSString *areaString = [NSString stringWithContentsOfFile:pathOfArea encoding:NSUTF8StringEncoding error:nil];
        NSArray *seporateredAreaArr = [areaString componentsSeparatedByString:@"\n"];
        
        NSMutableArray *finalAreaArr = [NSMutableArray array];
        for (NSString *areaString in seporateredAreaArr) {
            if (![areaString hasPrefix:@" "]) {
                Province *pro = [Province AreaWithName:areaString];
                [finalAreaArr addObject:pro];
            } else if ([areaString hasPrefix:@"  "] && ![areaString hasPrefix:@"    "]) {
                Province *pro = [finalAreaArr lastObject];
                City *city = [City AreaWithName:areaString];
                [pro.areaArr addObject:city];
            } else if ([areaString hasPrefix:@"    "]) {
                Province *pro = [finalAreaArr lastObject];
                City *city = [pro.areaArr lastObject];
                [city.areaArr addObject:areaString];
            }
        }
        //location文件处理
        NSString *pathOfLocation = @"/Users/dllo/DLS160710-09/Objective-C/HomeWork/07-homework/Location1.txt";
        NSString *locationStr = [NSString stringWithContentsOfFile:pathOfLocation encoding:NSUTF8StringEncoding error:nil];
        NSArray *locationArr = [locationStr componentsSeparatedByString:@","];
       
        NSMutableArray *tempLocationArr = [NSMutableArray array];
        for (NSString *locationString in locationArr) {
            NSArray *seporateredLocationArr = [locationString componentsSeparatedByString:@"|"];
            TempCity *tempCity = [[TempCity alloc] initWithCityName:[seporateredLocationArr firstObject] cityNumber:[seporateredLocationArr lastObject]];
            [tempLocationArr addObject:tempCity];
        }
        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
        [tempLocationArr sortUsingDescriptors:@[sort]];
        NSMutableArray *finalLocationArr = [NSMutableArray array];
        for (TempCity *tempCity in tempLocationArr) {
            if ([tempCity.cityNumber hasSuffix:@"0000"]) {
                Province *pro = [Province AreaWithName:tempCity.cityName number:tempCity.cityNumber];
                [finalLocationArr addObject:pro];
            } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
                Province *pro = [finalLocationArr lastObject];
                City *city = [City AreaWithName:tempCity.cityName number:tempCity.cityName];
                [pro.areaArr addObject:city];
            } else {
                Province *pro = [finalLocationArr lastObject];
                City *city = [pro.areaArr lastObject];
                District *dis = [District AreaWithName:tempCity.cityName number:tempCity.cityNumber];
                [city.areaArr addObject:dis];
            }
        }
        NSLog(@"%@", finalLocationArr);
        
        
        
    }
    return 0;
}
